<?php

class DefaultController extends Controller
{
    public function actionIndex()
	{


        $tree = Category::model()->buildTree('/subkatalog');
        $this->render('index', array(
            'tree' => $tree
        ));
	}

    public function actionList($id){
        $model = $this->loadModel($id);
        if (!isset(Yii::app()->request->cookies['id']) || Yii::app()->request->cookies['id']->value != $id ) {
            unset(Yii::app()->request->cookies['brand']);
            unset(Yii::app()->request->cookies['priceFrom']);
            unset(Yii::app()->request->cookies['priceTo']);
            Yii::app()->request->cookies['id'] = new CHttpCookie('id', $id);
        }

        $message = null;
        $subTree = null;
        $list = null;

        $brandDropDownList = array();
        $brandDropDownList[0] = 'Все';
        $criteria = new CDbCriteria();
        $criteria->condition = 'category_id = :category_id';
        $criteriaParams[':category_id'] = $id;
        $criteria->order = 't.title';



        if (isset($_POST[ 'brand' ])) {
            $brand = $_POST[ 'brand' ];
            Yii::app()->request->cookies['brand'] = new CHttpCookie('brand', $brand);
        }  else {
            $brand = 0;
            if (Yii::app()->request->cookies['brand']) $brand = Yii::app()->request->cookies['brand']->value;
        }

        if (isset($_POST[ 'priceFrom' ])) {
            $priceFrom = $_POST[ 'priceFrom' ] + 0;
            Yii::app()->request->cookies['priceFrom'] = new CHttpCookie('priceFrom', $priceFrom);
        }  else {
            $priceFrom = 0;
            if (Yii::app()->request->cookies['priceFrom']) $priceFrom = Yii::app()->request->cookies['priceFrom']->value + 0;
        }

        if (isset($_POST[ 'priceTo' ])) {
            $priceTo = $_POST[ 'priceTo' ] + 0;
            Yii::app()->request->cookies['priceTo'] = new CHttpCookie('priceTo', $priceTo);
        }  elseif(Yii::app()->request->cookies['priceTo']) {
            $priceTo = Yii::app()->request->cookies['priceTo']->value + 0;
        } else {
            $priceTo = 0;
        }

        if ($brand) {
            $criteria->addCondition('brand = :brand');
            $criteriaParams[':brand'] = $brand;
        }

        if ($priceFrom) {
            $criteria->addCondition('t.price >= :priceFrom');
            $criteriaParams[':priceFrom'] = $priceFrom;
        }

        if ($priceTo) {
            $criteria->addCondition('t.price <= :priceTo');
            $criteriaParams[':priceTo'] = $priceTo;
        }

        if (isset($_POST[ 'prodPerPage' ])) {
            $prodPerPage = $_POST[ 'prodPerPage' ];
            Yii::app()->request->cookies['prodPerPage'] = new CHttpCookie('prodPerPage', $prodPerPage);
        } elseif (Yii::app()->request->cookies['prodPerPage']) {
            $prodPerPage = Yii::app()->request->cookies['prodPerPage']->value;
        } else {
            $prodPerPage = 10;
        }

        $criteria->params = $criteriaParams;

        $dataProvider = new CActiveDataProvider('Product', array(
            'criteria'=>$criteria,
            'pagination'=>array(
                'pageSize'=>$prodPerPage,
            ),
        ));
        if ($model->products) {
            foreach ($model->products as $product){
                if ($product->brand)
                $brandDropDownList[$product->brand] = $product->brand;
            }
        }
        elseif ($model->categories) {
            $subTree = Category::model()->buildSubTree($model, '/subkatalog');
        }
        else {
            $message = 'В этой категории нет товаров.';
        }
        $this->render('list', array(
            'model' => $model,
			'list' => $dataProvider,
            'subTree' => $subTree,
            'message' => $message,
            'brand'=>$brand,
            'priceFrom'=>$priceFrom,
            'priceTo'=>$priceTo,
            'prodPerPage'=>$prodPerPage,
            'brandDropDownList' => $brandDropDownList
			)
        );
    }

    public function loadModel($id)
    {
        $model=Category::model()->findByPk($id);
        if($model===null)
            throw new CHttpException(404,'The requested page does not exist.');
        return $model;
    }
}
